class Solution {
public:
    int retsum(int n)
    {
        int sum = 0;
        while (n)
        {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        unordered_set<int> sset;
        while (1)
        {
            int ret = retsum(n);
            if (ret == 1)
                return true;
            else
            {
                if (sset.find(ret) != sset.end())
                    return false;
                sset.insert(ret);
            }
            n = ret;
        }
    }
};